package com.xq.jltx.adapter;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xq.jltx.db.NoteDbUtils;
import com.xq.jltx.db.bean.Note;
import com.xq.jltx.greendaopro.R;
import com.xq.jltx.listener.OnItemListener;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/21.
 */
public class ModefyNoteAdapter extends RecyclerViewCursorAdapter<RecyclerView.ViewHolder>{
    private Context context;
    private LayoutInflater mLayoutInflater;
    private OnItemListener mOnItemListener;
    private RecyclerView recyclerView;
    private int selectIndex = -1;

    public ModefyNoteAdapter(Context context, RecyclerView recyclerView, Cursor c) {
        super(context, c);
        this.context = context;
        this.recyclerView = recyclerView;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public OnItemListener getmOnItemListener() {
        return mOnItemListener;
    }

    public void setmOnItemListener(OnItemListener mOnItemListener) {
        this.mOnItemListener = mOnItemListener;
    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        int oldSelectIndex = this.selectIndex;
        this.selectIndex = selectIndex;
        if(oldSelectIndex != -1 && oldSelectIndex != selectIndex){
            notifyItemChanged(oldSelectIndex);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NoteTextViewHolder noteTextViewHolder = new NoteTextViewHolder(context,mLayoutInflater.inflate(R.layout.add_note_item_layout,parent,false));
        return noteTextViewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, Cursor cursor) {
        NoteTextViewHolder noteTextViewHolder = (NoteTextViewHolder)holder;
        TextView textView1 = noteTextViewHolder.get(R.id.note_text1);
        TextView textView2 = noteTextViewHolder.get(R.id.note_text2);
        Note note = NoteDbUtils.getNoteFromCursor(cursor);
        if(note != null){
            textView1.setText(note.getText());
            textView2.setText(note.getComment());
        }

        noteTextViewHolder.setSelect(false);
        int pos = holder.getLayoutPosition();
        if(selectIndex != -1 && selectIndex == pos){
            noteTextViewHolder.setSelect(true);
        }

        noteTextViewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                long id = holder.getItemId();
                if(mOnItemListener != null){
                    mOnItemListener.onItemClick(holder.itemView,pos,id);
                }

                int oldSelectIndex = selectIndex;
                selectIndex = pos;
                if(oldSelectIndex != -1 && oldSelectIndex != pos){
                    notifyItemChanged(oldSelectIndex);
                }
                notifyItemChanged(selectIndex);
            }
        });
        noteTextViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                long id = holder.getItemId();
                if(mOnItemListener != null){
                    mOnItemListener.onItemLongClick(holder.itemView,pos,id);
                }
                return false;
            }
        });
    }

    @Override
    protected void onContentChanged() {

    }

    /**
     *
     */
    public static class NoteTextViewHolder extends RecyclerView.ViewHolder{
        private SparseArray<View> viewHolders;
        private Context context;
        public NoteTextViewHolder(Context context,View itemView) {
            super(itemView);
            assignView();
            this.context = context;
        }

        private void assignView(){
            if(viewHolders == null){
                viewHolders = new SparseArray<>();
            }else {
                viewHolders.clear();
            }
        }

        public  <T extends View> T get(int id){
            View childView = viewHolders.get(id);
            if(null == childView){
                childView = itemView.findViewById(id);
                viewHolders.put(id,childView);
            }
            return (T) childView;
        }

        public void setSelect(boolean isSelect){
            if(isSelect){
                itemView.setSelected(isSelect);
            }else{
                itemView.setSelected(isSelect);

            }
        }
    }

}
